﻿using System;
using StackExchange.Redis;


namespace ThinNet.Redis
{
    /// <summary>
    /// Redis 操作类
    /// </summary>
    public class RedisHelper
    {
        private static ConnectionMultiplexer _redis;
       
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="connectionString"></param>
        public static void Init(string connectionString)
        {
            _redis = ConnectionMultiplexer.Connect(connectionString);
        }

        /// <summary>
        /// 释放
        /// </summary>
        public static void Dispose()
        {
            _redis.Dispose();
        }

        internal static IDatabase GetDatabase()
        {
            return _redis.GetDatabase();
        }

        #region Keys

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool KeyExists(string key)
        {
            return GetDatabase().KeyExists(key);
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Remove(string key)
        {
            return GetDatabase().KeyDelete(key);
        }

        /// <summary>
        /// 重命名
        /// </summary>
        /// <param name="key"></param>
        /// <param name="newKey"></param>
        /// <returns></returns>
        public static bool Rename(string key, string newKey)
        {
            return GetDatabase().KeyRename(key, newKey);
        }

        #endregion

        #region  for match

        /// <summary>
        /// 递减
        /// </summary>
        /// <param name="key"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        public static long Decrement(string key, long step = 1)
        {
            return GetDatabase().StringDecrement(key, step);
        }

        /// <summary>
        /// 递增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        public static long Increment(string key, long step = 1)
        {
            return GetDatabase().StringIncrement(key, step);
        }

        #endregion

        #region string

        public static bool SetString(string key, string value, int seconds = 60)
        {
            return SetString(key, value, new TimeSpan(0, 0, seconds));
        }

        public static bool SetString(string key, string value, TimeSpan span)
        {
            return GetDatabase().StringSet(key, value,  span);
        }

        public static string GetString(string key)
        {
            var value = GetDatabase().StringGet(key);
            if (value == RedisValue.Null)
            {
                return null;
            }
            return value.ToString();
        }

        public static long StringAppend(string key, string value)
        {
            return GetDatabase().StringAppend(key, value);
        }

        #endregion


        #region list 集合



        #endregion

        #region set 非重复集合



        #endregion
    }
}
